package com.glsc.ngateway.platform.amqp;

import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.other.Message;
import com.glsc.ngateway.platform.service.other.MessageService;
import com.glsc.ngateway.platform.utils.MqUtil;
import com.glsc.ngateway.platform.utils.RequestTool;
import com.rabbitmq.client.Channel;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息处理类切面
 */
@Aspect
@Component
public class MQReceiverAspect {

    private static final Logger logger = LoggerFactory.getLogger(MQReceiverAspect.class);

    @Resource
    private MessageService messageService;

    @Pointcut("execution(* com.glsc.ngateway.platform.amqp..*..*(org.springframework.amqp.core.Message, com.rabbitmq.client.Channel))")
    public void all() {

    }

    @Around("all()")
    public Object aroundAdvice(ProceedingJoinPoint point) throws Throwable {
        boolean ackFlag = true;
        Exception exception = null;
        Object[] args = point.getArgs();
        org.springframework.amqp.core.Message messageObject = (org.springframework.amqp.core.Message) args[0];
        com.rabbitmq.client.Channel channel = (Channel) args[1];
        String message = new String(messageObject.getBody());
        logger.info("plarform，收到消息，message receiver:" + message);
        Message bean = new Message();
        bean.setMsgType(DictConstant.MSG_TYPE_SYNC);
        bean.setMsgContent(message);
        bean.setMsgStatus(DictConstant.MSG_STATUS_SUCCESS);
        bean.setMsgSender(RequestTool.getLocalHost());
        messageService.saveMessage(bean);
        JSONObject msgJsonObject = JSONObject.parseObject(message);
//        args = new Object[]{args[0], args[1], message, msgJsonObject};
        //流程节点变化，获取事件通知时，主动延迟再去获取表单数据，待网关延迟功能上线后，此处不需要再延迟消费
        try {
            Thread.sleep(5 * 1000);//
        } catch (InterruptedException e) {
            logger.error("流程节点变化，获取事件通知时，主动延迟再去获取表单数据sleep失败", e);
            Thread.currentThread().interrupt();
        } catch (Exception e) {
            logger.error("err. ",e);
        }
        Object result = new Object();
        try {
            FlowReceiver.MESSAGE_THREAD_LOCAL.set(message);
            FlowReceiver.MSG_JSON_OBJ_THREAD_LOCAL.set(msgJsonObject);
            result = point.proceed(args);
        } catch (Exception e) {
            ackFlag = false;
            exception = e;
            logger.error("收到流程数据消息，处理异常：", e);
        } finally {
            FlowReceiver.MESSAGE_THREAD_LOCAL.remove();
            FlowReceiver.MSG_JSON_OBJ_THREAD_LOCAL.remove();
        }
        //消息发送是否异常,ackFlag=false，注意：网关的OA通知队列没有增加死信队列支持
        MqUtil.processAckFlag(messageObject, channel, ackFlag, "国联技术服务平台流程处理", exception);
        return result;
    }
}
